<template>
  <div>
    <el-dialog
      title="权限信息"
      :visible.sync="dialogVisible"
      width="600px"
      destroy-on-close
    >
      <div>
        <el-tree
          ref="tree"
          :data="treeData"
          show-checkbox
          node-key="oid"
          :props="defaultProps"
        >
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menuService, roleService } from "@/api/sysService";

export default {
  dicts: [],
  components: {},

  data() {
    return {
      resolve: null,
      dialogVisible: false,

      roleId: "",

      treeData: [],

      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },

  created() {
    // alert(1);
  },
  methods: {
    // loadData(node, resolve) {},
    open(row) {
      console.log("rolemenudialog");
      // console.log(row);
      this.roleId = row?.id;

      menuService.list({}).then((res) => {
        console.log("aaaa", res.data);
        this.treeData = res.data;

        roleService.getMenusByRoleId(this.roleId).then((res1) => {
          console.log("aaaa", res1.data);
          this.$refs.tree.setCheckedKeys(res1?.data || []);
        });
      });

      this.dialogVisible = true;
      // row && (this.form = row);
      return new Promise((resolve) => {
        this.resolve = resolve;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      // console.log(this.$refs.tree.getCheckedKeys());
      // console.log(this.$refs.tree.getHalfCheckedKeys());

      let selectNodeIds = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys(),
      ];

      roleService.postSavemenus(this.roleId, selectNodeIds).then((res) => {
        this.$modal.msgSuccess("操作成功");
        this.dialogVisible = false;
        // this.getList();
        this.resolve();
      });

      // this.$refs["form"].validate((valid) => {
      //   if (valid) {
      //     ctypeService.postSave(this.form).then((response) => {
      //       this.$modal.msgSuccess("操作成功");
      //       this.dialogVisible = false;
      //       // this.getList();
      //       this.resolve(response.data);
      //     });
      //   }
      // });
    },

    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      // this.reset();
    },
  },
};
</script>

<style lang="scss" scoped></style>
